//获取店铺id
var id = getvl("id");

var type = getvl("type");//首页焦点图

var userId = sessionStorage.getItem("userId");
//店铺详情
if(userId != null) {

	shopDetailById(id, userId,type);
} else {

	shopDetailById(id, '',type);
}

//分类
sortType();
//列表
loadMyEssay(id, '', '', '');

var commentLevel = '';
var isImg = '';

function shopDetailById(id, userId,type) {
	$("#shopList").html("");
	$("#serviceLevel").html("");
	$("#qualityLevel").html("");
	$("#carLevel").html("");
	$.ajax({
		url: urlcore + "/api/shopInfo/selectOne?id=" + id + "&userId=" + userId+"&type="+type,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;

				//店铺等级
				var storeLevelImg = '';
				var storeLevelName = '';
				if(da.storeLevel == 1) {
					storeLevelName = '普通店铺';
					storeLevelImg = '<img src="images/grade_tong.png" />';
				} else if(da.storeLevel == 2) {
					storeLevelName = '银牌店铺';
					storeLevelImg = '<img src="images/grade_yin.png" />';
				} else if(da.storeLevel == 3) {
					storeLevelName = '金牌店铺';
					storeLevelImg = '<img src="images/grade_jin.png" />';
				}
				//信誉等级
				var creditLevelImg = '';
				if(da.creditLevel == 1) {
					creditLevelImg = '<img src="images/aixiin.png" />';
				} else if(da.creditLevel == 2) {
					creditLevelImg = '<img src="images/aixiin.png" /><img src="images/aixiin.png" />';
				} else if(da.creditLevel == 3) {
					creditLevelImg = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
				} else if(da.creditLevel == 4) {
					creditLevelImg = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
				} else if(da.creditLevel == 5) {
					creditLevelImg = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
				}

				var monthNew = da.monthNew == null ? 0 : da.monthNew;
				var lookTime = da.lookTime == null ? 0 : da.lookTime;
				var rebuyRate = da.rebuyRate == null ? 0 : da.rebuyRate;
				var saleNumber = da.saleNumber == null ? 0 : da.saleNumber;
				var province=da.province==null?"":da.province;
				var city=da.city==null?"":da.city;
				var address=da.address==null?"":da.address;
				
				var serviceLevel=da.serviceLevel==null?0:da.serviceLevel;
				var qualityLevel=da.qualityLevel==null?0:da.qualityLevel;
				var carLevel=da.carLevel==null?0:da.carLevel;
				var shopNotice=da.shopNotice==null?"暂无":da.shopNotice;
				
				var areaUrl=da.tradeArea==null?"":da.tradeArea.areaUrl;
				areaUrl=isHasImg(areaUrl);
				var areaName=da.tradeArea==null?"":da.tradeArea.areaName;
				var newGoods=da.tradeArea==null?"":da.tradeArea.newGoods;
				var characteristic=da.tradeArea==null?"":da.tradeArea.characteristic;
				var briefIntroduction=da.tradeArea==null?"":da.tradeArea.briefIntroduction;
                var id=da.tradeArea==null?"":da.tradeArea.id;
				var thislist =
					'<div class="dianpu_xinxi_left">' +
					'<dl class="clearfix">' +
					'<dt>' +
					'<img src="' + isHasImg(da.headImg) + '" />' +
					'</dt>' +
					'<dd>' +
					'<h5>' + da.storeName + '</h5>' +
					'<ul>' +
					'<li>店铺等级：' +
					storeLevelImg +
					'<em>&nbsp;' + storeLevelName + '</em>' +
					'</li>' +
					'<li>信誉等级：' +
					creditLevelImg +
					'</li>' +
					'<li>' + province + '-' + city + '-' + address + '</li>' +
					'<li class="clearfix">' +
					'<span>开店时间：<em>' + da.gmtDatetime + '</em></span>' +
					'<span>本月上新：<em>' + monthNew + '</em></span>' +
					'<span>本月访问：<em>' + lookTime + '</em></span>' +
					'<span>复购率：<em>' + rebuyRate + '%</em></span>' +
					'<span>销量：<em>' + saleNumber + '</em></span>' +
					'</li>' +
					'<li class="clearfix">' +
					'<span>商家服务：<em>' + serviceLevel + '.0</em></span>' +
					'<span>商家品质：<em>' + qualityLevel + '.0</em></span>' +
					'<span>物流服务：<em>' + carLevel + '.0</em></span>' +
					'</li>' +
					'<li>店铺介绍：' +
					'<p>' + shopNotice + '</p>' +
					'</li>' +
					'</ul>' +
					'<div class="dianpu_guanzhu">' +
					'<span class="public-chat">联系客服</span>' +
					'<span class="Shoucang"></span>' +
					'</div>' +
					'</dd>' +
					'</dl>' +
					'</div>' +
					'<div class="dianpu_xinxi_right">' +
					'<h6>商圈信息</h6>' +
					'<dl class="clearfix">' +
					'<dt>' +
					'<a href="8.0.0shangquan.html?id=' + id + '"><img src="' + areaUrl + '" /></a>' +
					'</dt>' +
					'<dd>' +
					'<p>商圈名称：<a href="8.0.0shangquan.html?id=' + id + '"><span>' + areaName + '</span></a></p>' +
					'<p>本月更新：<em>' + newGoods + '</em></p>' +
					'<p>商圈特色：<span>' + characteristic + '</span></p>' +
					'<p>商圈介绍：<span>' + briefIntroduction + '</span></p>' +
					'</dd>' +
					'</dl>' +
					'</div>';
				$('#shopList').append(thislist);

				var focus = da.focusShop == null ? '' : da.focusShop.status;
				//关注店铺
				if(focus == 1) {
					$(".Shoucang").text('取消关注');
				} else {
					$(".Shoucang").text('关注');
				}
				//弹窗二维码
				$(function() {
					//隐藏登录页
					$(".closeLogin").click(function() {
						$(".public_simi_bg1").fadeOut();
					});

					//弹出
					$('.public-chat').on('click', function() {
						$(".public_simi_bg").fadeIn();
					});
					$(".public_simi_bg").on('click', function() {
						$(".public_simi_bg").fadeOut();
					})
					//收藏
					$(".Shoucang").click(function() {

						if(userId == '') {
							//显示登录页
							$(".public_simi_bg1").fadeIn();
						} else {
							var id = da.focusShop == null ? '' : da.focusShop.id;

							if($(this).text() == '关注') {

								if(id == '') {
									//取消关注
									if(!layer.confirm("确认关注吗?")) {
										return;
									}
									$.ajax({
										url: urlcore + '/api/focusShop/add',
										type: "post",
										contentType: "application/json;charset=utf-8",
										data: JSON.stringify({
											"shopId": da.id,
											"status": 1
										}),
										dataType: "json",
										success: function(data) {
											if(data.success == true) {
												layer.alert("关注成功");

												//跳转列表页
												window.location.href = "10.1.0dianpu_detail.html?id=" + da.id;
											} else if(data.code == 'OVERTIME') {
												var thisUrl = window.location.href;
												if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
													top.window.location.href = "1.0.0denglu.html";
												}
											} else {
												layer.alert(data.msg);
											}

										},
										error: function() {
											layer.alert("error");
										}

									});
								} else {
									//取消关注
									if(!layer.confirm("确认关注吗?")) {
										return;
									}
									$.ajax({
										url: urlcore + '/api/focusShop/update',
										type: "post",
										contentType: "application/json;charset=utf-8",
										data: JSON.stringify({
											"id": id,
											"status": 1
										}),
										dataType: "json",
										success: function(data) {
											if(data.success == true) {
												layer.alert("关注成功");

												//跳转列表页
												window.location.href = "10.1.0dianpu_detail.html?id=" + da.id;
											} else if(data.code == 'OVERTIME') {
												var thisUrl = window.location.href;
												if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
													top.window.location.href = "1.0.0denglu.html";
												}
											} else {
												layer.alert(data.msg);
											}

										},
										error: function() {
											layer.alert("error");
										}

									});
								}

							} else {

								//关注
								if(!layer.confirm("确认取消关注吗?")) {
									return;
								}
								$.ajax({
									url: urlcore + '/api/focusShop/update',
									type: "post",
									contentType: "application/json;charset=utf-8",
									data: JSON.stringify({
										"id": id,
										"status": 2
									}),
									dataType: "json",
									success: function(data) {
										if(data.success == true) {
											layer.alert("取消成功");

											//跳转列表页
											window.location.href = "10.1.0dianpu_detail.html?id=" + da.id;
										} else if(data.code == 'OVERTIME') {
											var thisUrl = window.location.href;
											if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
												top.window.location.href = "1.0.0denglu.html";
											}
										} else {
											layer.alert(data.msg);
										}

									},
									error: function() {
										layer.alert("error");
									}

								});
							}
						}

					});
				});

				//服务等级
				var serviceLevelName = '';
				if(da.serviceLevel == 1) {
					serviceLevelName = '<img src="images/star_select.png" />';
				} else if(da.serviceLevel == 2) {
					serviceLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.serviceLevel == 3) {
					serviceLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.serviceLevel == 4) {
					serviceLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.serviceLevel == 5) {
					serviceLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				}
				$("#serviceLevel").append(serviceLevelName);

				//质量等级
				var qualityLevelName = '';
				if(da.qualityLevel == 1) {
					qualityLevelName = '<img src="images/star_select.png" />';
				} else if(da.qualityLevel == 2) {
					qualityLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.qualityLevel == 3) {
					qualityLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.qualityLevel == 4) {
					qualityLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.qualityLevel == 5) {
					qualityLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				}
				$("#qualityLevel").append(qualityLevelName);
				//质量等级
				var carLevelName = '';
				if(da.carLevel == 1) {
					carLevelName = '<img src="images/star_select.png" />';
				} else if(da.carLevel == 2) {
					carLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.carLevel == 3) {
					carLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.carLevel == 4) {
					carLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				} else if(da.carLevel == 5) {
					carLevelName = '<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />&nbsp;<img src="images/star_select.png" />';
				}
				$("#carLevel").append(carLevelName);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

var goodsType = ''; //商品类别
var sortTypeId = ''; //商品分类
var sort = ''; //排序
var currentPage = 1;

function loadMyEssay(id, goodsType, sortTypeId, sort) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goodsList").html("");
		$.ajax({
			url: urlcore + "/api/goods/selectGoodsList?shopId=" + id + "&goodsType=" + goodsType + "&sortTypeId=" + sortTypeId + "&sort=" + sort + "&size=" + 12 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						var coverUrl=n.coverUrl==null?"":n.coverUrl;
						coverUrl=isHasImg(coverUrl);
						var goodsTitle=n.goodsTitle==null?"":n.goodsTitle;
						var tradePrice=n.tradePrice==null?"":n.tradePrice;
						var packPrice=n.packPrice==null?"":n.packPrice;
						var collectNum=n.collectNum==null?"":n.collectNum;
						var thislist =
							'<dl class="dianpu_goods">' +
							'<a href="2.2.0goods_detail.html?id=' + n.id + '">' +
							'<dt>' +
							'<img src="' + coverUrl + '" />' +
							'</dt>' +
							'<dd>' +
							'<h5>' + goodsTitle + '</h5>' +
							'<h6>&yen;' + tradePrice + '-' + packPrice + '<span>收藏 ' + collectNum + '</span>' + '</h6>' +
							'</dd>' +
							'</a>' +
							'</dl>';

						$('#goodsList').append(thislist);
					});

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

//按类型查询
function selectGoodsByGoodsType(goodsType) {
	$("#commentToShop").hide();
	$("#tiaojian").show();
	loadMyEssay(id, goodsType, '', '');
}

//按分类查询
function searchBySortType(sortTypeId) {
	$("#commentToShop").hide();
	$("#tiaojian").show();
	loadMyEssay(id, '', sortTypeId, '');
}

//排序
function goodsSorts(sort) {
	$("#commentToShop").hide();
	$("#tiaojian").show();
	loadMyEssay(id, '', '', sort);
}

function selectGoodsByComment() {
	$("#tiaojian").hide();
	$("#commentToShop").show();

	//计算评论数量
	getCommentCount(id);

	//加载评论信息
	loadMyEssayByComment(id, '', '');

}

function getCommentCount(id) {
	$("#selectComment0").html("");
	$("#selectComment1").html("");
	$("#selectComment2").html("");
	$("#selectComment3").html("");
	$("#selectComment4").html("");
	$.ajax({
		url: urlcore + "/api/comment/selectCount?shopId=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;

				if(da.hpSize != null && da.hpSize != '' && da.total != null && da.total != '') {
					//好评率
					var hp = da.hpSize / da.total * 100;

					$("#haoping").text(divsub(hp.toFixed(2)));
				}

				var selectCommentByLevel0 = '全部(' + da.total + ')';
				$("#selectComment0").text(selectCommentByLevel0);

				var selectCommentByLevel1 = '好评(' + da.hpSize + ')';
				$("#selectComment1").text(selectCommentByLevel1);

				var selectCommentByLevel2 = '中评(' + da.zpSize + ')';
				$("#selectComment2").text(selectCommentByLevel2);

				var selectCommentByLevel3 = '差评(' + da.cpSize + ')';
				$("#selectComment3").text(selectCommentByLevel3);

				var selectCommentByLevel4 = '晒图(' + da.stSize + ')';
				$("#selectComment4").text(selectCommentByLevel4);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

var currentPage = 1;
//列表
function loadMyEssayByComment(id, commentLevel, isImg) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#commentList1").html("");
		$.ajax({
			url: urlcore + "/api/comment/getCommentByCommentLevel?shopId=" + id + "&commentLevel=" + commentLevel + "&isImg=" + isImg + "&size=" + 20 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {

						var commentImg = '';
					//	if(n.commentImg.indexOf("***") != -1) {
						if(n.commentImg!=null){
							var imgArr = n.commentImg.split("***");
							$.each(imgArr, function(i, img) {
								img=isHasImg(img);
								commentImg += '<img src="' + img + '" />';
							});
						}
							
						//}

						var qualityLevel = '';
						if(n.qualityLevel == 1) {
							qualityLevel = '<img src="images/star_select.png" /><em>1.0分</em>';
						} else if(n.qualityLevel == 2) {
							qualityLevel = '<img src="images/star_select.png" /><img src="images/star_select.png" /><em>2.0分</em>';
						} else if(n.qualityLevel == 3) {
							qualityLevel = '<img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><em>3.0分</em>';
						} else if(n.qualityLevel == 4) {
							qualityLevel = '<img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><em>4.0分</em>';
						} else if(n.qualityLevel == 5) {
							qualityLevel = '<img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><img src="images/star_select.png" /><em>5.0分</em>';
						}

						var thislist =
							'<dl class="clearfix">' +
							'<dt>' +
							'<img src="' + n.user.headImg + '" />' +
							'</dt>' +
							'<dd class="detail_pingjia">' +
							'<h6>' + n.user.userName + '</h6>' +
							'<span>' + new Date(n.gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss") + '</span>' +
							'<p>' + n.commentContent + '</p>' +
							'<div class="dianpu_pingjia_xinxi_star clearfix">' + qualityLevel + '</div>' +
							'<div class="dianpu_pingjia_xinxi_img">' + commentImg + '</div>' +
							'</dd>' +
							'</dl>';
						$('#commentList1').append(thislist);
					});

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

function selectComment(commentLevel, isImg) {
	loadMyEssayByComment(id, commentLevel, isImg);
}

function getLogin1() {
	var phone = $('#phone').val().trim();
	if(phone == '') {
		layer.alert("请输入手机号码！");
		return false;
	}
	var password = $('#password').val().trim();
	if(password == '') {
		layer.alert("请输入登录密码！");
		return false;
	}
	$.ajax({
		url: urlcore + "/api/user/loginByPhone",
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"phone": phone,
			"password": password
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				if(layer.confirm("登录成功！")) {

					var da = data.data;
					//var url="index.html?userName="+da.userName;
					//window.location.href = encodeURI(url);

					if(window.sessionStorage) {
						//登录名
						sessionStorage.setItem("userName", da.userName);
						//登录类型
						sessionStorage.setItem("userType", da.userType);
						sessionStorage.setItem("userId", da.id);
						//设置cookie
						/*var domain_url = urlcore.split('://')[1];
						if (domain_url.indexOf(':') > -1) {
							domain_url = domain_url.split(':')[0];
						}else{
							domain_url = domain_url.split('/')[0];
						}
						document.cookie = "JSESSIONID="+data.data.token+";path=/;domain="+domain_url;*/

						//	sessionStorage.setItem("userId",da.id);
						window.location.href = "10.1.0dianpu_detail.html?id=" + id;
					} else {
						alert("浏览暂不支持sessionStorage");
					}

					//或者 if(typeof window.localStorage == 'undefined'){ 	alert("浏览暂不支持localStorage") }
					//刷新
					//location.reload();

				} else {
					//跳转列表页
					window.location.href = "1.0.0denglu.html";
				}
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}
		},
		error: function() {
			layer.alert("error");
		}

	});
}